var qqmapsdk, app = getApp(), Toast = require("../../libs/zanui/toast/toast"), Toptips = require("../../libs/zanui/toptips/index"), QQMapWX = require("../../libs/qqmap-wx-jssdk.js");

Page({
    data: {
        showConfirm: !1,
        placeholder: "点击获取定位",
        isAgree: !0,
        play: !1,
        files: [],
        video: [],
        cateIndex: 0,
        album: ""
    },
    onLoad: function(t) {
        var a = this;
        app.viewCount(), wx.getStorageSync("userInfo") ? (t.id && (a.setData({
            item_id: t.id
        }), a.getEditData(t.id)), a.getBasicInfo()) : a.setData({
            showLogin: !0
        }), app.util.footer(a);
    },
    getUserInfo: function(t) {
        var a = this;
        "getUserInfo:ok" == t.detail.errMsg ? (a.setData({
            showLogin: !1
        }), app.util.getUserInfo(function() {
            a.getBasicInfo();
        }, t.detail)) : wx.navigateTo({
            url: "../home/index"
        });
    },
    closeModal: function() {
        this.setData({
            showLogin: !1
        }), wx.navigateTo({
            url: "../home/index"
        });
    },
    closeAuth: function() {
        this.setData({
            showAuth: !1
        });
    },
    getBasicInfo: function() {
        var o = this;
        app.util.request({
            url: "entry/wxapp/item",
            cachetime: "0",
            data: {
                act: "get",
                m: "superman_hand2"
            },
            success: function(t) {
                if (t.data.errno) o.showIconToast(t.errmsg); else {
                    var a = t.data.data, e = a.category, i = [];
                    if (0 < e.length) for (var s = 0; s < e.length; s++) i.push(e[s].title);
                    o.setData({
                        video_switch: a.video_switch,
                        category: i,
                        district: a.district,
                        rule: a.rule
                    });
                }
            }
        });
    },
    showPopup: function() {
        var t = this;
        t.setData({
            showBottomPopup: !0
        });
        var a = t.data.rule;
        require("../../libs/wxParse/wxParse.js").wxParse("article", "html", a, t, 5), t.setData({
            rule: rule
        });
    },
    toggleBottomPopup: function() {
        this.setData({
            showBottomPopup: !this.data.showBottomPopup
        });
    },
    getEditData: function(t) {
        var n = this;
        app.util.request({
            url: "entry/wxapp/item",
            cachetime: "0",
            data: {
                act: "edit",
                id: t,
                m: "superman_hand2"
            },
            success: function(t) {
                if (t.data.errno) n.showIconToast(t.errmsg); else {
                    var a = t.data.data, e = a.video || [];
                    n.setData({
                        detail: a,
                        files: a.album,
                        video: e,
                        videoPath: e.join(","),
                        address: a.address,
                        cate: a.category,
                        videoFinish: !0,
                        finish: !0
                    });
                    for (var i = n.data.files, s = [], o = 0; o < i.length; o++) s[o] = i[o].substring(i[o].indexOf("images/"));
                    n.setData({
                        album: s.join(",")
                    });
                }
            }
        });
    },
    getAddress: function() {
        var n = this;
        n.data.item_id || (n.setData({
            placeholder: "正在定位中..."
        }), qqmapsdk = new QQMapWX({
            key: "7M4BZ-FDEK3-JC737-YXQ5O-V3IO3-E5FH5"
        }), wx.getSetting({
            success: function(t) {
                var a = t.authSetting["scope.userLocation"];
                "undefined" != a && !1 === a && (n.setData({
                    showAuth: !0
                }), n.setData({
                    placeholder: "点击获取定位"
                }));
            }
        }), wx.getLocation({
            type: "gcj02",
            success: function(t) {
                var a = t.latitude, e = t.longitude;
                n.setData({
                    lat: a,
                    lng: e
                }), qqmapsdk.reverseGeocoder({
                    location: {
                        latitude: a,
                        longitude: e
                    },
                    get_poi: 1,
                    poi_options: "address_format=short;radius=2000;policy=1;category=房产小区,住宅区,住宅小区",
                    success: function(t) {
                        var a = t.result.formatted_addresses.recommend, e = n.data.district;
                        if (0 == e.length) n.setData({
                            address: a
                        }); else {
                            for (var i = t.result.pois, s = 0; s < e.length; s++) for (var o = 0; o < i.length; o++) if (i[o].title.match(e[s].title)) {
                                n.setData({
                                    address: e[s].title
                                });
                                break;
                            }
                            "正在定位中..." == n.data.placeholder && n.setData({
                                address: a
                            });
                        }
                    },
                    fail: function(t) {
                        Toptips(t.errmsg);
                    }
                });
            },
            fail: function() {
                Toptips("自动定位失败，请手动填写所在小区"), n.setData({
                    placeholder: "请填写所在小区"
                });
            }
        }));
    },
    showCategory: function(t) {
        this.data.cate && this.setData({
            cate: ""
        }), this.setData({
            cateIndex: t.detail.value
        });
    },
    bindAgreeChange: function(t) {
        this.setData({
            isAgree: !!t.detail.value.length
        });
    },
    chooseVideo: function() {
        var a = this;
        wx.chooseVideo({
            sourceType: [ "album", "camera" ],
            maxDuration: 60,
            success: function(t) {
                a.setData({
                    video: a.data.video.concat(t.tempFilePath),
                    videoFinish: !1
                }), a.uploadVideo(t.tempFilePath);
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    uploadVideo: function(t) {
        var e = this, a = app.util.url("entry/wxapp/item", {
            act: "upload",
            m: "superman_hand2"
        });
        wx.uploadFile({
            url: a,
            filePath: t,
            name: "videoData",
            fail: function(t) {
                console.log(t);
            },
            success: function(t) {
                var a = JSON.parse(t.data);
                0 == a.errno ? (e.setData({
                    videoFinish: !0
                }), e.data.videoPath ? e.setData({
                    videoPath: e.data.videoPath + "," + a.data
                }) : e.setData({
                    videoPath: a.data
                })) : Toptips(a.errmsg);
            }
        });
    },
    deleteVideo: function(s) {
        var o = this;
        o.data.videoFinish ? wx.showModal({
            title: "提示",
            content: "确定要删除该视频吗？",
            success: function(t) {
                if (t.confirm) {
                    var a = s.currentTarget.dataset.index, e = o.data.video;
                    if (e.splice(a, 1), o.setData({
                        video: e
                    }), o.data.videoPath) {
                        var i = o.data.videoPath.split(",");
                        i.splice(a, 1), 0 < i.length ? o.setData({
                            videoPath: i.join(",")
                        }) : o.setData({
                            videoPath: ""
                        });
                    }
                }
            }
        }) : Toptips("视频正在上传，请稍后再试");
    },
    chooseImage: function(t) {
        var i = this;
        wx.chooseImage({
            sizeType: [ "original", "compressed" ],
            sourceType: [ "album", "camera" ],
            success: function(t) {
                i.setData({
                    files: i.data.files.concat(t.tempFilePaths),
                    finish: !1
                });
                var a = t.tempFilePaths.length, e = app.util.url("entry/wxapp/item", {
                    act: "upload",
                    m: "superman_hand2"
                });
                i.uploadImg(t.tempFilePaths, 0, a, e);
            }
        });
    },
    uploadImg: function(e, i, s, o) {
        var n = this;
        wx.uploadFile({
            url: o,
            filePath: e[i],
            name: "imgData",
            fail: function(t) {
                console.log(t);
            },
            success: function(t) {
                i++;
                var a = JSON.parse(t.data);
                0 == a.errno ? n.data.album ? n.setData({
                    album: n.data.album + "," + a.data
                }) : n.setData({
                    album: a.data
                }) : Toptips(a.errmsg), i != s ? n.uploadImg(e, i, s, o) : n.setData({
                    finish: !0
                });
            }
        });
    },
    deleteImg: function(t) {
        var a = this;
        if (a.data.finish) {
            var e = t.currentTarget.dataset.index, i = a.data.files;
            if (i.splice(e, 1), a.setData({
                files: i
            }), a.data.album) {
                var s = a.data.album.split(",");
                s.splice(e, 1), a.setData({
                    album: s.join(",")
                });
            }
        } else Toptips("图片正在上传，请稍后再试");
    },
    formSubmit: function(t) {
        var a = this, e = t.detail.value, i = e.address, s = t.detail.formId, o = a.data.cateIndex, n = a.data.category, d = a.data.cate, r = a.data.album, l = a.data.videoPath || "", c = "";
        a.data.item_id && (c = a.data.item_id), e.title ? o || d ? i ? a.data.isAgree ? app.util.request({
            url: "entry/wxapp/item",
            cachetime: "0",
            method: "POST",
            data: {
                act: "post",
                id: c,
                title: e.title,
                description: e.description,
                album: r,
                video: l,
                category: d || n[o],
                address: i,
                price: e.price,
                formid: s,
                lat: a.data.lat,
                lng: a.data.lng,
                m: "superman_hand2"
            },
            success: function(t) {
                t.data.errno ? Toptips(t.data.errmsg) : (a.showIconToast("发布成功", "success"), setTimeout(function() {
                    wx.navigateTo({
                        url: "../home/index"
                    });
                }, 1500));
            }
        }) : Toptips("请阅读公约") : Toptips("请自动定位或手动填写所在小区") : Toptips("请选择分类") : Toptips("请填写标题");
    },
    showIconToast: function(t) {
        var a = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : "fail";
        Toast({
            type: a,
            message: t,
            selector: "#zan-toast"
        });
    }
});